package com.example.a001.part17;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.a001.R;

public class mainActivity02 extends AppCompatActivity implements View.OnClickListener {

    private Button btnAnonymous;
    private Button btnExternal;
    private Button btnInterface;
    private Button btnLayoutBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main017_1);

        // 初始化控件
        btnAnonymous = findViewById(R.id.btn_anonymous); // 确保布局文件中有此ID
        btnExternal = findViewById(R.id.btn_external);
        btnInterface = findViewById(R.id.btn_interface);
        btnLayoutBind = findViewById(R.id.btn_layout_bind);

        // 匿名内部类事件监听
        btnAnonymous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mainActivity02.this, "匿名内部类监听被触发", Toast.LENGTH_SHORT).show();
            }
        });

        // 外部类事件监听
        btnExternal.setOnClickListener(new MyOnclickListener());

        // 接口实现事件监听
        btnInterface.setOnClickListener(this);
    }

    // 布局绑定事件监听对应的方法
    public void myClick(View source) {
        Toast.makeText(this, "布局绑定监听被触发", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        if (v == btnInterface) {
            Toast.makeText(this, "接口实现监听被触发", Toast.LENGTH_SHORT).show();
        }
    }

    // 外部类事件监听类
    class MyOnclickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Toast.makeText(mainActivity02.this, "外部类监听被触发", Toast.LENGTH_SHORT).show();
        }
    }
}
